function jsUpload(id,maxsize,callback=false) {
    var images = $("#upload_"+id).val();
    var $file = event.currentTarget;
    lrz($file.files[0],{width: 800,quality:0.7,fieldName:'images'})
    .then(function (rst) {
        // getScaleSize($file,rst);
        var resultSize = (rst.fileLen/1024).toFixed(0);
        if (resultSize > maxsize) {
            mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
            $("#upload_"+id).val('');
            return;
        }
        $("#upload_"+id).prev().text("上传中");
        // 额外添加参数
        rst.formData.append("fileLen", rst.fileLen);
        var users_id = $("#users_id").val();
        var links_id = $("#links_id").val();
        var sign = $("#sign").val();
        var token = $("#token").val();
        var avatar = '';
        if(id == 'msg_logo' || id == 'button_image' || id == 'logo' || id == 'index_banner'){
            avatar = 'avatar';
        }else if(id == 'qrcode'){
            var platfrom_type = links_lists.platfrom_type;
            var platfrom = links_lists.platfrom;
            rst.formData.append("platfrom_type", platfrom_type);
            rst.formData.append("platfrom", platfrom);
        }
        rst.formData.append("users_id", users_id);
        rst.formData.append("links_id", links_id);
        rst.formData.append("sign", sign);
        rst.formData.append("avatar", avatar);
        rst.formData.append("token", token);
        $.ajax({
            url: "/api/version/1/upload/base_upload",
            type: "POST",
            dataType: "json",
            data: rst.formData,
            contentType: false,
            processData: false,
            success:function(data){
                if (data.status == "success") {
                    $("#"+id+"_show").attr("src",data.url);
                    $("#"+id).trigger('change');
                    $("#"+id).val(data.url);
                    if (callback) {
                        jsUploadCallBack(data.url);
                    }
                } else {
                    if (data.code == 1002) {
                        mviewToastr("error",data.msg);
                        setTimeout(function (){
                            window.location.href="/admin/login?msg="+data.msg;
                        }, 3000);
                    } else {
                        mviewToastr("error",data.msg);
                    }
                }
                $("#upload_"+id).prev().text("选择图片");
            },
            error:function(err){
                mviewToastr("error","网络异常,请联系客服处理！");
                $("#upload_"+id).prev().text("选择图片");
            }
        })
    })
    .catch(function (err) {
        mviewToastr("error","网络异常,请联系客服处理！");
    });
}

function jsUploadMore(id,maxsize,callback=false) {
    var images = $("#upload_"+id).val();
    var $file = event.currentTarget;
    lrz($file.files[0],{width: 800,quality:0.7,fieldName:'images'})
    .then(function (rst) {
        // getScaleSize($file,rst);
        var resultSize = (rst.fileLen/1024).toFixed(0);
        if (resultSize > maxsize) {
            mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
            $("#upload_"+id).val('');
            return;
        }
        // 额外添加参数
        rst.formData.append("fileLen", rst.fileLen);
        var users_id = $("#users_id").val();
        var links_id = $("#links_id").val();
        var sign = $("#sign").val();
        var token = $("#token").val();
        var avatar = 'avatar';
        rst.formData.append("avatar", avatar);
        rst.formData.append("users_id", users_id);
        rst.formData.append("links_id", links_id);
        rst.formData.append("sign", sign);
        rst.formData.append("token", token);
        $("#upload_"+id).prev().text("上传中");
        $.ajax({
            url: "/api/version/1/upload/base_upload",
            type: "POST",
            dataType: "json",
            data: rst.formData,
            contentType: false,
            processData: false,
            success:function(data){
                if (data.status == "success") {
                    var imageid = Math.ceil(Math.random()*10000000);
                    $("#"+id+"_lists").prepend('<div id="'+imageid+'"><img src="'+data.url+'" class="file-border-view-lists"><i class="file-cotainer-icon iconfont icon-guanbi" onclick=\'deleteImages("'+id+'","'+data.url+'","'+imageid+'");\'></i></div>');
                    var msg_qrcodes = $("#msg_qrcodes").val();
                    if (msg_qrcodes == "") {
                        $("#"+id).val(data.url);
                    } else {
                        $("#"+id).val(msg_qrcodes+","+data.url);
                    }
                    if (callback) {
                        jsUploadCallBack(data.url);
                    }
                } else {
                    if (data.code == 1002) {
                        mviewToastr("error",data.msg);
                        setTimeout(function (){
                            window.location.href="/admin/login?msg="+data.msg;
                        }, 3000);
                    } else {
                        mviewToastr("error",data.msg);
                    }
                }
                $("#upload_"+id).prev().text("添加图片");
            },
            error:function(err){
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        })
    })
    .catch(function (err) {
        mviewToastr("error","网络异常,请联系客服处理！");
    });
}

function jsQrcodeUpload(id,maxsize,callback=false) {
    var images = $("#upload_"+id).val();
    var $file = event.currentTarget;
    var origin_type = $file.files[0].type;
    var file = $file.files;
    if ($.inArray(origin_type,["image/png","image/jpeg","image/webp"]) < 0) {
        mviewToastr("error","当前不支持上传此类型图片,请上传JPG/PNG/Webp图片!");
        $("#upload_"+id).val('');
        return;
    }
    $("#upload_"+id+"_text").text("压缩中");
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function (e) {
        var data = e.target.result;
        //加载图片获取图片真实宽度和高度
        var image = new Image();
        image.src = data;
        // 图片先加载完，才可以得到图片宽度和高度
        image.onload = function(){
            var width = image.width;
            var height = image.height;
            var pressParamas = {};
            if (width > 800) {
                pressParamas.width = 800;
            }
            if (file[0].size >= 1000000) {
                pressParamas.size = 300;
                pressParamas.quality = 0.8;
            } else if (file[0].size >= 500000) {
                pressParamas.size = 200;
                pressParamas.quality = 0.8;
            } else {
                pressParamas.size = 100;
                pressParamas.quality = 0.8;
            }
            if ($.inArray(origin_type,["image/png","image/webp"]) < 0) {
                pressParamas.type = file[0].type;
            } else {
                pressParamas.type = "image/jpeg";
            }
            imageConversion.compress($file.files[0],pressParamas).then(pressResult=>{
                var pressImageUrl = URL.createObjectURL(pressResult);
                var pressFile = blobToFile(pressResult,file[0].name,file[0].type);
                var oldSize = (file[0].size/1024).toFixed(0);
                var resultSize = (pressFile.size/1024).toFixed(0);

                if (resultSize > maxsize) {
                    mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
                    $("#upload_"+id).val('');
                    $("#upload_"+id+"_text").text("上传");
                    return;
                }
                var users_id = $("#users_id").val();
                var links_id = $("#links_id").val();
                var sign = $("#sign").val();
                var token = $("#token").val();

                var formData = new FormData();
                formData.append("images", pressFile);
                formData.append("users_id", users_id);
                formData.append("links_id", links_id);
                formData.append("platfrom_type", links_lists.platfrom_type);
                formData.append("platfrom", links_lists.platfrom);
                formData.append("sign", sign);
                formData.append("token", token);

                $.ajax({
                    url: "/api/version/1/upload/base_upload",
                    type: "POST",
                    data: formData,
                    contentType:false,
                    processData:false,
                    success:function(data){
                        if (data.status == "success") {
                            let qrcodes_id = Math.round(new Date().getTime()/1000).toString()+Math.ceil(Math.random()*10000).toString();
                            var expiration_time = moment().add(2, "years").format("YYYY-MM-DD");
                            links_lists.qrcodeLists.push({"qrcodes_id":qrcodes_id,"id":id,"img_url":data.url,"name":qrcodes_id,"expiration_time":expiration_time,"count_limit":"1000000","rate":"10"});
                            renderQrcodes(id);
                        } else {
                            if (data.code == 1002) {
                                mviewToastr("error",data.msg);
                                setTimeout(function (){
                                    window.location.href="/admin/login?msg="+data.msg;
                                }, 3000);
                            } else {
                                mviewToastr("error",data.msg);
                            }
                        }
                        $("#upload_"+id).prev().text("添加二维码");
                    },
                    error:function(err){
                        mviewToastr("error","网络异常,请联系客服处理！");
                    }
                })
            });
        }
    }
}


function blobToFile(blob,name,type) {
    return new window.File([blob],name,{"type":type});
}

function jsQrcodeUploadImage(id,maxsize) {
    var images = $("#upload_"+id).val();
    var $file = event.currentTarget;
    lrz($file.files[0],{width: 800,quality:0.7,fieldName:'images'})
    .then(function (rst) {
        // getScaleSize($file,rst);
        var resultSize = (rst.fileLen/1024).toFixed(0);
        if (resultSize > maxsize) {
            mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
            $("#upload_"+id).val('');
            return;
        }
        // 额外添加参数
        rst.formData.append("fileLen", rst.fileLen);
        var users_id = $("#users_id").val();
        var links_id = $("#links_id").val();
        var sign = $("#sign").val();
        var token = $("#token").val();
        rst.formData.append("users_id", users_id);
        rst.formData.append("links_id", links_id);
        rst.formData.append("sign", sign);
        rst.formData.append("token", token);
        $("#upload_"+id).prev().text("上传中");
        $.ajax({
            url: "/api/version/1/upload/base_upload",
            type: "POST",
            dataType: "json",
            data: rst.formData,
            contentType: false,
            processData: false,
            success:function(data){
                if (data.status == "success") {
                    let qrcodes_id = $("#qrcodes_id").val();
                    $.each(links_lists.qrcodeLists,function(key,qrcodeList){
                        if (qrcodeList.qrcodes_id == qrcodes_id) {
                            qrcodeList.img_url = data.url;
                        }
                    });
                    $("#"+qrcodes_id+" img").attr("src",data.url);
                    $("#qrcode_show").attr("src",data.url);
                    renderQrcodes();
                } else {
                    if (data.code == 1002) {
                        mviewToastr("error",data.msg);
                        setTimeout(function (){
                            window.location.href="/admin/login?msg="+data.msg;
                        }, 3000);
                    } else {
                        mviewToastr("error",data.msg);
                    }
                }
                $("#upload_"+id).prev().text("更新");
            },
            error:function(err){
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        })
    })
    .catch(function (err) {
        mviewToastr("error","网络异常,请联系客服处理！");
    });
}

function jsUploadStep(id,maxsize,stepid,type) {
    var images = $("#upload_"+id).val();
    var $file = event.currentTarget;
    lrz($file.files[0],{width: 800,quality:0.7,fieldName:'images'})
    .then(function (rst) {
        // getScaleSize($file,rst);
        var resultSize = (rst.fileLen/1024).toFixed(0);
        if (resultSize > maxsize) {
            mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
            $("#upload_"+id).val('');
            return;
        }
        // 额外添加参数
        rst.formData.append("fileLen", rst.fileLen);
        var users_id = $("#users_id").val();
        var links_id = $("#links_id").val();
        var sign = $("#sign").val();
        var token = $("#token").val();
        rst.formData.append("users_id", users_id);
        rst.formData.append("links_id", links_id);
        rst.formData.append("sign", sign);
        rst.formData.append("token", token);
        $.ajax({
            url: "/api/version/1/upload/base_upload",
            type: "POST",
            dataType: "json",
            data: rst.formData,
            contentType: false,
            processData: false,
            success:function(data){
                if (data.status == "success") {
                    $("#"+id+"_show").attr("src",data.url);
                    $("#"+id).val(data.url);
                    changeStep(stepid,type);
                } else {
                    if (data.code == 1002) {
                        mviewToastr("error",data.msg);
                        setTimeout(function (){
                            window.location.href="/admin/login?msg="+data.msg;
                        }, 3000);
                    } else {
                        mviewToastr("error",data.msg);
                    }
                }
            },
            error:function(err){
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        })
    })
    .catch(function (err) {
        mviewToastr("error","网络异常,请联系客服处理！");
    });
}

function uploadWeixinMedia(id,maxsize,callback=false) {
    var appid = $("#appid").val();
    var users_id = $("#users_id").val();
    var $file = event.currentTarget;
    lrz($file.files[0],{width: 800,quality:0.7,fieldName:'media'})
    .then(function (rst) {
        // getScaleSize($file,rst);
        var resultSize = (rst.fileLen/1024).toFixed(0);
        if (resultSize > maxsize) {
            mviewToastr("error","图片压缩后不能超过"+maxsize+"KB,当前为"+resultSize+"KB。");
            $("#upload_"+id).val('');
            return;
        }
        // 额外添加参数
        rst.formData.append("fileLen", rst.fileLen);
        rst.formData.append("users_id", users_id);
        rst.formData.append("appid", appid);
        $.ajax({
            url: "/api/version/1/upload/upload_miniapp_media",
            type: "POST",
            dataType: "json",
            data: rst.formData,
            contentType: false,
            processData: false,
            success:function(data){
                if (data.status == "success") {
                    var medias_images_src = getObjectURL($file.files[0]) ; 
                    $("#"+id+"_show").attr("src",medias_images_src).show();
                    $("#"+id).val(data.media_id);
                    if (callback) {
                        jsUploadCallBack(data.media_id);
                    }
                } else {
                    mviewToastr("error",data.msg);
                }
            },
            error:function(err){
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        })
    })
    .catch(function (err) {
        mviewToastr("error","网络异常,请联系客服处理！");
    });
}

function getObjectURL(file) {
    var url = null ;
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}

function getScaleSize(that,rst){
    var sourceSize = (that.files[0].size/1024).toFixed(2);
    var resultSize = (rst.fileLen/1024).toFixed(2);
    var scale = parseInt(100 - (resultSize / sourceSize * 100));
    console.log("源文件:"+sourceSize+"KB\r\n压缩后:"+resultSize+"KB\r\n节省:"+scale+"%");
}
